package com.zhaotianhao.controller;

import com.zhaotianhao.pojo.Users;
import com.zhaotianhao.service.UsersService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * @Author Z-7
 * @Date 2022/3/1
 */
@Controller
@RequestMapping("user")
public class UserController {
    @DubboReference
    private UsersService usersService;
    @GetMapping("login")
    public String to_login(Model model){
        model.addAttribute("user",new Users());
        return "users/user_login";
    }
    @PostMapping("login")
    public String login(@Valid @ModelAttribute("user")Users users, BindingResult result , HttpSession session){
        if(result.hasErrors()){
            return "users/user_login";
        }
        Users login_user = usersService.login(users.getUsername(),users.getPassword());
        if(login_user == null){
            return "users/user_login";
        }
        session.setAttribute("LOGIN_USER",login_user);
        return "redirect:../goods/list";
    }
    @RequestMapping("loginOut")
    public String loginOut(HttpSession session){
        // 注销登录
        session.invalidate();
        return "redirect:/login";
    }
}
